/****************************************************************************
 Copyright (c) 2022-2023 Xiamen Yaji Software Co., Ltd.

 https://www.cocos.com/

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights to
 use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
 of the Software, and to permit persons to whom the Software is furnished to do so,
 subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
****************************************************************************/

#include "core/builtin/BuiltinResMgr.h"
#include "core/assets/EffectAsset.h"
#include "core/assets/ImageAsset.h"
#include "core/assets/Material.h"
#include "core/assets/Texture2D.h"
#include "core/assets/TextureCube.h"
#include "math/Color.h"
#include "platform/Image.h"
#include "rapidjson/document.h"
#include "renderer/core/ProgramLib.h"
#include "scene/Pass.h"

namespace cc {

namespace {
constexpr uint8_t BLACK_IMAGE_RGBA_DATA_2X2[2 * 2 * 4] = {
    // r, g, b, a
    0x00, 0x00, 0x00, 0xFF,
    0x00, 0x00, 0x00, 0xFF,
    0x00, 0x00, 0x00, 0xFF,
    0x00, 0x00, 0x00, 0xFF};

constexpr uint8_t GREY_IMAGE_RGBA_DATA_2X2[2 * 2 * 4] = {
    0x77, 0x77, 0x77, 0xFF,
    0x77, 0x77, 0x77, 0xFF,
    0x77, 0x77, 0x77, 0xFF,
    0x77, 0x77, 0x77, 0xFF};

constexpr uint8_t WHITE_IMAGE_RGBA_DATA_2X2[2 * 2 * 4] = {
    0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF,
    0xFF, 0xFF, 0xFF, 0xFF};

constexpr uint8_t NORMAL_IMAGE_RGBA_DATA_2X2[2 * 2 * 4] = {
    0x7F, 0x7F, 0xFF, 0xFF,
    0x7F, 0x7F, 0xFF, 0xFF,
    0x7F, 0x7F, 0xFF, 0xFF,
    0x7F, 0x7F, 0xFF, 0xFF};

constexpr uint8_t EMPTY_IMAGE_RGBA_DATA_2X2[2 * 2 * 4] = {
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00,
    0x00, 0x00, 0x00, 0x00};

const uint8_t DEFAULT_IMAGE_RGBA_DATA_16X16[16 * 16 * 4] = {
    0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF,
    0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF,
    0xDD, 0xDD, 0xDD, 0xFF, 0xDD, 0xDD, 0xDD, 0xFF, 0xDD, 0xDD, 0xDD, 0xFF, 0xDD, 0xDD, 0xDD, 0xFF,
    0xDD, 0xDD, 0xDD, 0xFF, 0xDD, 0xDD, 0xDD, 0xFF, 0xDD, 0xDD, 0xDD, 0xFF, 0xDD, 0xDD, 0xDD, 0xFF,
    0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF,
    0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF,
    0xDD, 0xDD, 0xDD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xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF,
    0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF, 0x55, 0x55, 0x55, 0xFF};

} // namespace

BuiltinResMgr *BuiltinResMgr::instance = nullptr;

/* static */
BuiltinResMgr *BuiltinResMgr::getInstance() {
    return BuiltinResMgr::instance;
}

BuiltinResMgr::BuiltinResMgr() {
    BuiltinResMgr::instance = this;
}

BuiltinResMgr::~BuiltinResMgr() {
    BuiltinResMgr::instance = nullptr;
}

void BuiltinResMgr::addAsset(const ccstd::string &uuid, Asset *asset) {
    _resources.emplace(uuid, asset);
}

Asset *BuiltinResMgr::getAsset(const ccstd::string &uuid) {
    auto iter = _resources.find(uuid);
    if (iter != _resources.end()) {
        return iter->second.get();
    }

    return nullptr;
}

bool BuiltinResMgr::initBuiltinRes() {
    if (_isInitialized) {
        return true;
    }

    _isInitialized = true;

    // NOTE:  C++ use const array to store color value, no need to synchronize ts's valueView logic

    // black texture
    initTexture2DWithUuid("black-texture", BLACK_IMAGE_RGBA_DATA_2X2, sizeof(BLACK_IMAGE_RGBA_DATA_2X2), 2, 2);

    // empty texture
    initTexture2DWithUuid("empty-texture", EMPTY_IMAGE_RGBA_DATA_2X2, sizeof(EMPTY_IMAGE_RGBA_DATA_2X2), 2, 2);

    // grey texture
    initTexture2DWithUuid("grey-texture", GREY_IMAGE_RGBA_DATA_2X2, sizeof(GREY_IMAGE_RGBA_DATA_2X2), 2, 2);

    // white texture
    initTexture2DWithUuid("white-texture", WHITE_IMAGE_RGBA_DATA_2X2, sizeof(WHITE_IMAGE_RGBA_DATA_2X2), 2, 2);

    // normal texture
    initTexture2DWithUuid("normal-texture", NORMAL_IMAGE_RGBA_DATA_2X2, sizeof(NORMAL_IMAGE_RGBA_DATA_2X2), 2, 2);

    // default texture
    initTexture2DWithUuid("default-texture", DEFAULT_IMAGE_RGBA_DATA_16X16, sizeof(DEFAULT_IMAGE_RGBA_DATA_16X16), 16, 16);

    // black cube texture
    initTextureCubeWithUuid("black-cube-texture", BLACK_IMAGE_RGBA_DATA_2X2, sizeof(BLACK_IMAGE_RGBA_DATA_2X2), 2, 2);

    // grey cube texture
    initTextureCubeWithUuid("grey-cube-texture", GREY_IMAGE_RGBA_DATA_2X2, sizeof(GREY_IMAGE_RGBA_DATA_2X2), 2, 2);

    // white cube texture
    initTextureCubeWithUuid("white-cube-texture", WHITE_IMAGE_RGBA_DATA_2X2, sizeof(WHITE_IMAGE_RGBA_DATA_2X2), 2, 2);

    // default cube texture
    initTextureCubeWithUuid("default-cube-texture", DEFAULT_IMAGE_RGBA_DATA_16X16, sizeof(DEFAULT_IMAGE_RGBA_DATA_16X16), 16, 16);

    //cjh TODO:    if (SpriteFrame) {
    //        const spriteFrame = ccnew SpriteFrame() as SpriteFrame;
    //        const image = imgAsset;
    //        const texture = ccnew Texture2D();
    //        texture.image = image;
    //        spriteFrame.texture = texture;
    //        spriteFrame._uuid = 'default-spriteframe";
    //        resources[spriteFrame->getUuid()] = spriteFrame;
    //    }
    //
    //
    //    return Promise.resolve().then(() => {

    // initMaterials();

    return true;
}

void BuiltinResMgr::initTexture2DWithUuid(const ccstd::string &uuid, const uint8_t *data, size_t dataBytes, uint32_t width, uint32_t height) {
    IMemoryImageSource imageSource;
    imageSource.width = width;
    imageSource.height = height;
    imageSource.data = ccnew ArrayBuffer(data, static_cast<uint32_t>(dataBytes));
    imageSource.compressed = false;
    imageSource.format = PixelFormat::RGBA8888;

    auto *texture = ccnew Texture2D();
    if (texture) {
        texture->setUuid(uuid);

        auto *imgAsset = ccnew ImageAsset();
        imgAsset->setNativeAsset(imageSource);
        texture->setImage(imgAsset);

        texture->initialize();
        _resources.emplace(texture->getUuid(), texture);
    }
}

void BuiltinResMgr::initTextureCubeWithUuid(const ccstd::string &uuid, const uint8_t *data, size_t dataBytes, uint32_t width, uint32_t height) {
    IMemoryImageSource imageSource;
    imageSource.width = width;
    imageSource.height = height;
    imageSource.data = ccnew ArrayBuffer(data, static_cast<uint32_t>(dataBytes));
    imageSource.compressed = false;
    imageSource.format = PixelFormat::RGBA8888;

    auto *texture = ccnew TextureCube();
    if (texture) {
        texture->setUuid(uuid);
        texture->setMipFilter(TextureCube::Filter::NEAREST);

        ITextureCubeMipmap mipmap;
        mipmap.front = ccnew ImageAsset();
        mipmap.front->setNativeAsset(imageSource);
        mipmap.back = ccnew ImageAsset();
        mipmap.back->setNativeAsset(imageSource);
        mipmap.left = ccnew ImageAsset();
        mipmap.left->setNativeAsset(imageSource);
        mipmap.right = ccnew ImageAsset();
        mipmap.right->setNativeAsset(imageSource);
        mipmap.top = ccnew ImageAsset();
        mipmap.top->setNativeAsset(imageSource);
        mipmap.bottom = ccnew ImageAsset();
        mipmap.bottom->setNativeAsset(imageSource);

        texture->setImage(&mipmap);

        texture->initialize();
        _resources.emplace(texture->getUuid(), texture);
    }
}

} // namespace cc
